package com.xlb.domain.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xlb.domain.dos.WikiDO;


import java.time.LocalDate;
import java.util.List;
import java.util.Objects;


/**
 * @author 小聋包
 * @date 2024/6/4
 * @Version 1.0
 */
public interface WikiMapper extends BaseMapper<WikiDO> {


    /**
     * 分页查询
     * @param current
     * @param size
     * @param title
     * @param startDate
     * @param endDate
     * @param isPublish
     * @return
     */
    default Page<WikiDO> selectPageList(Long current, Long size, String title, LocalDate startDate,LocalDate endDate,Boolean isPublish) {
        //分页对象(查询第几页，每页多少数据）
        Page<WikiDO> page = new Page<>(current, size);
        //构造查询条件
        LambdaQueryWrapper<WikiDO> wrapper = Wrappers.<WikiDO> lambdaQuery()
                .like(StringUtils.isNotBlank(title), WikiDO::getTitle, title)//like模块查询
                .ge(Objects.nonNull(startDate), WikiDO::getCreateTime, startDate)//大于等于startDate
                .le(Objects.nonNull(endDate), WikiDO::getCreateTime, endDate)//小于等于endDate
                .eq(Objects.nonNull(isPublish), WikiDO::getIsPublish, isPublish)//等于isPublish
                .orderByDesc(WikiDO::getWeight)//按权重倒叙
                .orderByDesc(WikiDO::getCreateTime);//按时间倒序

         return  selectPage(page,wrapper);
    }


    default  WikiDO selectMaxWeight(){
        return  selectOne(Wrappers.<WikiDO>lambdaQuery()
                .orderByDesc(WikiDO::getWeight)//按权重值降序排列
                .last("limit 1"));//仅查询一条
    }

    /**
     * 查询已发布的知识库
     * @return
     */
    default List<WikiDO> selectPublished(){
        return  selectList(Wrappers.<WikiDO>lambdaQuery()
                .eq(WikiDO::getIsPublish,1)//查询已发布 is_publish=1
                .orderByDesc(WikiDO::getWeight)//按权重值降序排列
                .orderByDesc(WikiDO::getCreateTime)//按时间降序排列
        );
    }
}
